#ifndef _BLES3_H_
#define _BLES3_H_

//#include "Equations/TwoPhaseEquation.hpp"
#include "./TwoPhaseEquation.hpp"

namespace Tuna {

  template <typename T, int Dim> class BLES3;

  template <typename T>
  struct Typeinfo <BLES3<T, 1> > {
    typedef T prec_t;
    enum { Dim = 1 };
  };

  template <typename T>
  struct Typeinfo <BLES3<T, 2> > {
    typedef T prec_t;
    enum { Dim = 2 };
  };

  template <typename T>
  struct Typeinfo <BLES3<T, 3> > {
    typedef T prec_t;
    enum { Dim = 3 };
  };
  
  template<typename Tprec, int Dim>
  class BLES3 : public TwoPhaseEquation<BLES3<Tprec, Dim> >
  {

    typedef TwoPhaseEquation<BLES3<Tprec, Dim> > TP_BLES3;

    using GeneralEquation< TP_BLES3 >::aE;
    using GeneralEquation< TP_BLES3 >::aW;
    using GeneralEquation< TP_BLES3 >::aN;
    using GeneralEquation< TP_BLES3 >::aS;
    using GeneralEquation< TP_BLES3 >::aF;
    using GeneralEquation< TP_BLES3 >::aB;
    using GeneralEquation< TP_BLES3 >::aP;
    using GeneralEquation< TP_BLES3 >::sp;
    using GeneralEquation< TP_BLES3 >::dx;
    using GeneralEquation< TP_BLES3 >::dy;
    using GeneralEquation< TP_BLES3 >::dz;
    using GeneralEquation< TP_BLES3 >::dt;
    using GeneralEquation< TP_BLES3 >::bi;
    using GeneralEquation< TP_BLES3 >::ei;
    using GeneralEquation< TP_BLES3 >::bj;
    using GeneralEquation< TP_BLES3 >::ej;
    using GeneralEquation< TP_BLES3 >::bk;
    using GeneralEquation< TP_BLES3 >::ek;
    using GeneralEquation< TP_BLES3 >::applyBoundaryConditions1D;
    using GeneralEquation< TP_BLES3 >::applyBoundaryConditions2D;
    using GeneralEquation< TP_BLES3 >::applyBoundaryConditions3D;

    using TP_BLES3::S;
    using TP_BLES3::p;
    using TP_BLES3::phi_0;
    using TP_BLES3::Srw;
    using TP_BLES3::Sro;
    using TP_BLES3::mu_w;
    using TP_BLES3::mu_o;
    using TP_BLES3::k;
    using TP_BLES3::porosity;

  public:
    typedef Tprec prec_t;
    typedef typename TunaArray<prec_t, Dim >::huge ScalarField;
    
    BLES3() : TwoPhaseEquation<BLES3<prec_t, Dim > >() { }    
    ~BLES3() { };
    
    inline bool calcCoefficients1D(); 
    inline bool calcCoefficients2D() { };
    inline bool calcCoefficients3D() { };
    inline void printInfo() { std::cout << " BLES3 "; }
  };

/*
 *  Lineal for realtive permeability , Average for Sw
 */
template<typename Tprec, int Dim>
inline bool BLES3<Tprec, Dim>::calcCoefficients1D () 
{
    static prec_t Sw_e, Sw_w;

    // Lineal 
    static prec_t mult = k * dt / 
      ( porosity * dx * dx * (1 - Srw - Sro) * mu_w );
      
    aE = 0.0; aW = 0.0; aP = 0.0; sp = 0.0;

    for (int i =  bi; i <= ei; ++i) {      

      // Average
      Sw_e = ( phi_0(i+1) + phi_0(i) ) * 0.5;
      Sw_w = ( phi_0(i-1) + phi_0(i) ) * 0.5; 

      // Lineal
      aE (i) = (Sw_e - Srw) * mult;
      aW (i) = (Sw_w - Srw) * mult;
      aP (i) = aE (i) + aW (i);      
    }

    // Dirichlet right side
    aP(ei) += aE(ei);
    sp(ei) = 2 * aE(ei) * p(ei+1);
    aE(ei) = 0;

    // Neumann left side
    aP(bi) -= aW(bi) ;
    sp(bi) = aW(bi) * dx * ( 3.47e-7 * mu_w / k) ;
    aW(bi) = 0;

    return 0;
}



} // Tuna namespace


#endif //_BLES3_H_

















